package com.czl.module_storehouse.activity.scan;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.config.AppConstants;
import com.czl.base.event.LiveBusCenter;
import com.czl.base.util.PermissionUtil;
import com.czl.base.util.core.QRCodeView;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.activity.baseInfo.GoodsBaseInfoActivity;
import com.czl.module_storehouse.activity.baseInfo.WareSingleProductActivity;
import com.czl.module_storehouse.databinding.ActivityScanBinding;
import com.czl.module_storehouse.event.ScanSortEvent;
import com.czl.module_storehouse.utils.QrCodeRuleUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * @author by xubin,
 * @date on 2022/2/22.
 * PS: Not easy to write code, please indicate.
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_SCAN)
public class ScanActivity extends BaseActivity<ActivityScanBinding> implements QRCodeView.Delegate {
    public static final String RESULT = "result";

    @Override
    protected ActivityScanBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityScanBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("扫码");
        binding.zxingview.setDelegate(ScanActivity.this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        PermissionUtil.INSTANCE.reqCamera(this, null, (allGranted, grantedList, deniedList) -> {
            if (allGranted) {
                binding.zxingview.startCamera();
                binding.zxingview.changeToScanBarcodeStyle();
                //开始扫描
                binding.zxingview.showScanRect();
                binding.zxingview.startSpotDelay(200);
            } else {
                showToast("您拒绝了相机权限");
            }
        });
    }

    @Override
    protected void onStop() {
        binding.zxingview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        binding.zxingview.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        Intent intent = new Intent();
        intent.putExtra(RESULT, result);
        setResult(RESULT_OK, intent);
        Log.e(TAG, "扫码结果：" + result);
        String storehouseHome = getIntent().getStringExtra("storehouseHome");
        if (!TextUtils.isEmpty(storehouseHome)) {
            startProductActivity(result);
        }
        LiveBusCenter.INSTANCE.postScanResultReturnEvent(result);
        finish();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            vibrator.vibrate(200);
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    private void startProductActivity(String result) {
        String sort = QrCodeRuleUtils.handleResult(result, Constants.QRCodeRule.REMAP_SORT);
        Intent intent = new Intent();
        if (TextUtils.isEmpty(sort)) {
            intent.putExtra(RESULT, result);
            intent.setClass(getContext(), WareSingleProductActivity.class);
        } else {
            LiveEventBus.get(ScanSortEvent.class).postDelay(new ScanSortEvent(sort, null), 200);
            intent.setClass(getContext(), GoodsBaseInfoActivity.class);
        }
        startActivity(intent);
    }

}
